package com.whu.helpluojia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whu.helpluojia.dao.FindDao;
import com.whu.helpluojia.dao.LostAndFoundDao;
import com.whu.helpluojia.domain.*;
import com.whu.helpluojia.domain.dto.LafDto;
import com.whu.helpluojia.service.FindService;
import com.whu.helpluojia.service.LostAndFoundService;
import com.whu.helpluojia.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FindServiceImpl extends ServiceImpl<FindDao,Find> implements FindService {

    @Autowired
    FindDao findDao;

    @Autowired
    UserService userService;

//    @Autowired
//    LostAndFoundDao lostAndFoundDao;

    @Autowired
    LostAndFoundService lostAndFoundService;

    /**
     * 修改状态为已解决
     * @param find
     */
    @Override
    public void status(Find find) {
        find.setStatus(1);
        findDao.updateById(find);
    }

    @Override
    public void unmatched(Find find) {
        findDao.insert(find);
    }

    @Override
    public List<LafDto> insert(Find find) {
        List<LafDto> res = new ArrayList<>();
        LostAndFound[] lostAndFounds = lostAndFoundService.selectAll();
        for(LostAndFound lostAndFound : lostAndFounds){
            if(find.getLonMin() <= lostAndFound.getLon() && lostAndFound.getLon() <= find.getLonMax()
                    && find.getLatMin() <= lostAndFound.getLat() && lostAndFound.getLat() <= find.getLatMax()){
                LafDto lafDto=new LafDto();
                BeanUtils.copyProperties(lostAndFound,lafDto);
                User user=userService.selectUser(lafDto.getUserId());
                lafDto.setNickname(user.getNickname());
                lafDto.setSex(user.getGender());
                res.add(lafDto);
            }
        }
        findDao.insert(find);
        if(res.size() != 0) {
            return res;
        }
        return null;
    }

    @Override
    public void solute(Long id) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id);
        updateWrapper.set("status", 1);
        findDao.update(null, updateWrapper);
    }

    @Override
    public MyPublish getFindDetail(Long id, Long userId) {
        Find find=findDao.selectById(id);

        User user=userService.selectUser(userId);

        MyPublish myPublish=new MyPublish();

        myPublish=copyData(myPublish,find,user);

        return myPublish;
    }

    @Override
    public Find[] selectAll() {
//        QueryWrapper<Find> findQueryWrapper = new QueryWrapper<>();
//        findQueryWrapper.isNotNull("id");
        //return findDao.selectObjs(findQueryWrapper).toArray(new Find[0]);
        List<Find> list = findDao.selectList(null);
        Find[] array = new Find[list.size()];
        for(int i = 0;i < list.size();i++){
            array[i] = list.get(i);
        }
        return array;
    }

    @Override
    public List<MyPublish> getMyDetail(Long userId) {
        LambdaQueryWrapper<Find> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Find::getUserId,userId);
        List<Find> findList=findDao.selectList(lambdaQueryWrapper);

        User user=userService.selectUser(userId);

        List<MyPublish> myPublishes=new ArrayList<>();

        for(Find find:findList){
            MyPublish myPublish=new MyPublish();
            myPublish=copyData(myPublish,find,user);
            myPublishes.add(myPublish);
        }

        return myPublishes;
    }

    public MyPublish copyData(MyPublish myPublish,Find find,User user){
        myPublish.setId(find.getId().longValue());
        myPublish.setCategory("寻物启事");
        myPublish.setContent(find.getDescription());
        myPublish.setSex(user.getGender());
        myPublish.setStatus(find.getStatus());
        myPublish.setSenderName(user.getName());
        myPublish.setTime(find.getTime());
        myPublish.setType(find.getType());
        return myPublish;

    }

}
